package com.ktgj.service.controller;

import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.web.page.TableDataInfo;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.flight.api.RemoteFlightService;
import com.ktgj.service.domain.ContractLinkVipchannel;
import com.ktgj.service.domain.ContractLinkVipchannelPrice;
import com.ktgj.service.domain.Vipchannel;
import com.ktgj.service.domain.vo.ChannelVo;
import com.ktgj.service.domain.vo.VipHallVipChannelPrice;
import com.ktgj.service.domain.vo.VipchannelVo;
import com.ktgj.service.service.IVipchannelService;
import org.apache.poi.util.LittleEndianOutputStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * VIP通道Controller
 * 
 * @author ruoyi
 * @date 2021-09-03
 */
@ControllerAdvice
@RestController
@RequestMapping("/vipchannels")
public class VipchannelController extends BaseController
{
    @Autowired
    private IVipchannelService vipchannelService;

    /**
     * 查询VIP通道列表
     */
    //@PreAuthorize(hasPermi = "service:vipchannel:list")
    @GetMapping("/list")
    public TableDataInfo list(Vipchannel vipchannel)
    {
        startPage();
        List<Vipchannel> list = vipchannelService.selectVipchannelList(vipchannel);
        return getDataTable(list);
    }

    /**
     * 导出VIP通道列表
     */
    //@PreAuthorize(hasPermi = "service:vipchannel:export")
    @Log(title = "VIP通道 ", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Vipchannel vipchannel) throws IOException
    {
        List<Vipchannel> list = vipchannelService.selectVipchannelList(vipchannel);
        ExcelUtil<Vipchannel> util = new ExcelUtil<Vipchannel>(Vipchannel.class);
        util.exportExcel(response, list, "VIP通道 数据");
    }

    /**
     * 获取VIP通道详细信息
     */
    //@PreAuthorize(hasPermi = "service:vipchannel:query")
    @GetMapping(value = "/{vipchannelId}")
    public AjaxResult getInfo(@PathVariable("vipchannelId") Long vipchannelId)
    {
        return AjaxResult.success(vipchannelService.selectVipchannelById(vipchannelId));
    }

    /**
     * 新增VIP通道
     */
    //@PreAuthorize(hasPermi = "service:vipchannel:add")
    @Log(title = "VIP通道 ", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Vipchannel vipchannel)
    {
        return toAjax(vipchannelService.insertVipchannel(vipchannel));
    }

    /**
     * 修改VIP通道
     */
    //@PreAuthorize(hasPermi = "service:vipchannel:edit")
    @Log(title = "VIP通道 ", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Vipchannel vipchannel)
    {
        return toAjax(vipchannelService.updateVipchannel(vipchannel));
    }

    /**
     * 删除VIP通道
     */
    //@PreAuthorize(hasPermi = "service:vipchannel:remove")
    @Log(title = "VIP通道 ", businessType = BusinessType.DELETE)
	@DeleteMapping("/{vipchannelIds}")
    public AjaxResult remove(@PathVariable Long[] vipchannelIds)
    {
        return toAjax(vipchannelService.deleteVipchannelByIds(vipchannelIds));
    }

    /**
     * 根据合同查找对应的VIP通道
     */
    @PostMapping("getContractVipChannl")
    public AjaxResult getContractVipChannelList(Long contractId)
    {
        List<Vipchannel> lstVipChannel = vipchannelService.selectVipchannelAndContractList
                (contractId);
        return AjaxResult.success(lstVipChannel);
    }


    /**
     * 新增vip通道价格维护
     * @param vipchannel
     * @return
     */
    @Log(title = "VIP通道合同维护 ", businessType = BusinessType.INSERT)
    @PostMapping(value = "/addVipContractPrice")
    public AjaxResult addVipContractPrice(@RequestBody ContractLinkVipchannel vipchannel){
        //先验证vip通道中间表中是否存在已维护的价格
        int result = vipchannelService.selectVipChannelByContractIdAndVipChannelId(vipchannel.getContractId(),
                vipchannel.getVipchannelId());
        if(result > 0){
            return AjaxResult.error("该服务项目价格已存在!");
        }
        //执行vip通道价格维护操作
        return toAjax(vipchannelService.addVipChannelPrice(vipchannel));
    }

    /**
     * 查询VIP通道列表远程调用使用
     * @return
     */
    @GetMapping("/getVipchannelList")
    public List<Vipchannel> getVipchannelList(){
        List<Vipchannel> list = vipchannelService.getVipchannelList();
        return list;
    }

    @GetMapping("/queryVipchannel")
    public TableDataInfo queryVipchannel(@RequestParam(required = false, value = "vipchannelId") Long vipchannelId) {
        startPage();
        List<Map<String, Object>> mapList = vipchannelService.queryVipchannelMap(vipchannelId);
        return getDataTable(mapList);
    }

    /**
     * 根据机场三字码查询
     * */
    @PostMapping("/selectVipchannelByAirportCode")
    public boolean selectVipchannelByAirportCode(@RequestParam("airportCode") String airportCode){
        return vipchannelService.selectVipchannelByAirportCode(airportCode);
    }

    @PostMapping("/selectVipChannelPrice")
    public AjaxResult selectVipChannelPrice(@RequestParam("id") Long id) {
        VipHallVipChannelPrice price = vipchannelService.selectVipChannelPrice(id);
        return AjaxResult.success(price);
    }

    /**
     * vip通道基本数据列表
     * */
    @PostMapping("/selectVipchannelList")
    public TableDataInfo selectVipchannelList(@RequestBody ChannelVo channelVo) {
        startPage();
        List<Vipchannel> vipchannels;
        try {
            vipchannels = vipchannelService.selectVipchannelList(channelVo);
        } catch (Exception e){
            e.printStackTrace();
            return null;
        }
        return getDataTable(vipchannels);
    }

    /**
     * 添加VIP通道信息
     * */
    @PostMapping("/addVipchannel")
    public AjaxResult addVipchannel(@RequestBody Vipchannel vipchannel){
        int i = vipchannelService.addVipchannel(vipchannel);
        if (i > 0){
            return AjaxResult.success("添加成功");
        }
        else {
            return AjaxResult.error("添加失败");
        }
    }

    /**
     * 删除vip通道信息
     * */
    @PostMapping("/delVipchannel")
    public AjaxResult delVipchannel(@RequestBody Map<String, Object> map){
        Long id = Long.valueOf(map.get("id").toString());
        int i = vipchannelService.delVipchannel(id);
        if (i > 0){
            return AjaxResult.success("删除成功");
        }
        else {
            return AjaxResult.error("删除失败");
        }
    }
}
